// sd_liferuning/pages/constmer/balance-recharge/index.js
var api = require("../../../api.js"), app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    balance: 0,
    alipay: "",
    name: "",
    account: "",
    yue: 0,
    distribution: ''
  },
  onLoad: function (e) {
    console.log(e)
    this.setData({
      type: e.type,
      yue: e.yue,
      distribution: e.distribution
    })
  },
  onShow: function () {
    var that = this;
    //异步获取缓存值
    // wx.getStorage({
    //   key: 'menu_template',
    //   success(res) {
    //     console.log(res.data)
    //     that.setData({ balance: res.data.max_withdraw })
    //   }
    // })
    app.request({
      url: api.bank.get_maxmoney,
      method: 'post',
      data: {
        type: 1,
        default: 1
      },
      state: 1,
      success: function (e) {
        console.log(e)
        if (e.code == 1) {
          that.setData({
            data: e.data,
            //bank_number: e.data.banknum.slice(-4),
            balance: e.data.price,
          })
        }
      }
    })
  },
  get_alipay: function (e) {
    console.log(e.detail.value);
    this.setData({ alipay: e.detail.value });
  },
  get_name: function (e) {
    this.setData({ name: e.detail.value });
  },
  get_account(e) {
    this.setData({ money: e.detail.value });
  },
  sendRequest: function () {
    let _this = this;
    const { money, alipay, account, balance, name } = this.data;
    console.log(money, alipay, account);
    if (alipay == "") {
      wx.showToast({
        title: '支付宝账号不得为空',
        icon: 'none',
        mask: true
      })
      return false;
    }
    if (name == "") {
      wx.showToast({
        title: '请填写用户名',
        icon: 'none',
        mask: true
      })
      return false;
    }
    if (money == "") {
      wx.showToast({
        title: '请填写正确金额',
        icon: 'none',
        mask: true
      })
      return false;
    } else if (money < 1) {
      wx.showToast({
        title: '提现金额至少1元',
        icon: 'none',
        mask: true
      })
      return false;
    } else if (Number(money) > Number(_this.data.balance)) {
      wx.showToast({
        title: '金额大于可提现金额',
        icon: 'none',
        mask: true
      })
      return false;
    }

    console.log(money, balance, account)
    wx.showLoading({
      mask: true
    })
    var type = _this.data.type
    //  0提现到余额 1银行卡 2 支付宝 参数nickname：昵称  alipay：支付宝账号
    if (this.data.distribution == 1) {
      app.request({
        url: api.user.apply,
        method: 'post',
        data: {
          price: money,
          alipay: alipay,
          nickname: name,
          type: 2 //提现到银行卡
        },
        success: function (rs) {
          console.log('提现申请结果', rs)
          wx.hideLoading()
          if (rs.code == 1) {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true,
              duration: 2000,
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true
            })
          }
        }
      })
    } else if (this.data.distribution == 2) {
      app.request({
        url: api.distribution.apply,
        method: 'post',
        data: {
          price: money,
          alipay: alipay,
          nickname: name,
          type: 2 //提现到银行卡
        },
        success: function (rs) {
          console.log('提现申请结果', rs)
          wx.hideLoading()
          if (rs.code == 1) {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true,
              duration: 2000,
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true
            })
          }
        }
      })
    }

  }
})